package cn.wildfire.chat.kit.conversation.wallet;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProviders;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import cn.wildfire.chat.kit.AppService;
import cn.wildfire.chat.kit.R;
import cn.wildfire.chat.kit.R2;
import cn.wildfire.chat.kit.WfcBaseActivity;
import cn.wildfire.chat.kit.entry.RedResult;
import cn.wildfire.chat.kit.viewmodel.MessageViewModel;
import cn.wildfirechat.message.Message;
import cn.wildfirechat.message.TextMessageContent;
import cn.wildfirechat.message.notification.NotificationMessageContent;

public class SendPackgeActivity extends WfcBaseActivity {
    @BindView(R2.id.etNum) EditText etNum;
    @BindView(R2.id.etPrice) EditText etPrice;
    @BindView(R2.id.etNote) EditText etNote;
    @BindView(R2.id.tvPayment)
    TextView tvPayment;
    @BindView(R2.id.viewNum)
    View viewNum;
    @BindView(R2.id.radioGroup)
    RadioGroup radioGroup;
    @BindView(R2.id.radioButton1)
    RadioButton radioButton1;
    @BindView(R2.id.radioButton2)
    RadioButton radioButton2;
    private MessageViewModel messageViewModel;
    private String redType = "0";
    private boolean isGroup;
    private String targetId;
    private double totalAmount;
    private String num = "0";
    private String price = "0";

    @Override
    protected int contentLayout() {
        return R.layout.activity_send_packge;
    }
    @Override
    protected void afterViews() {
        super.afterViews();
        if (!isDarkTheme()) {
            setTitleBackgroundResource(R.color.white, false);
        }
        isGroup = getIntent().getBooleanExtra("isGroup",true);
        targetId = getIntent().getStringExtra("targetId");
        if (isGroup){
            redType = "1";
        }else {
            redType = "0";
            viewNum.setVisibility(View.GONE);
            radioGroup.setVisibility(View.GONE);
        }
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == radioButton1.getId()) {
                    redType = "1";
                } else if(checkedId == radioButton2.getId()){
                    redType = "2";
                }
                updateAmount();
            }
        });
        messageViewModel = ViewModelProviders.of(this).get(MessageViewModel.class);
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }
    @OnTextChanged({R2.id.etPrice,R2.id.etNum})
    public void onEditChange(Editable editable){
        num = etNum.getText().toString();
        price = etPrice.getText().toString();
        if (redType.equals("0")){
            num = "1";
        }else {
            if (TextUtils.isEmpty(num)) {
                return;
            }
        }
        if (TextUtils.isEmpty(price)){
            return;
        }
        updateAmount();
    }
    private void updateAmount(){
        if (redType.equals("0")){
            totalAmount = Double.valueOf(price);
        }else if (redType.equals("1")) {
            totalAmount = Double.valueOf(price);
        }else if (redType.equals("2")) {
            totalAmount = Integer.valueOf(num) * Double.valueOf(price);
        }
        tvPayment.setText("￥ "+totalAmount);
    }
    private void sendRed(String password){
        num = etNum.getText().toString();
        price = etPrice.getText().toString();
        String note = etNote.getText().toString();
        if (redType.equals("0")){
            num = "1";
        }else {
            if (TextUtils.isEmpty(num)) {
                return;
            }
        }
        if (TextUtils.isEmpty(price)){
            return;
        }
        if (TextUtils.isEmpty(note)){
            note = "恭喜发财大吉大利";
            return;
        }
        updateAmount();
//        类型 0个人 1群拼手气 2群普通红包
        String finalNote = note;
        AppService.Instance().postSendRed(password,targetId,String.valueOf(totalAmount),redType,num,note,new AppService.RedCallback() {
            @Override
            public void onUiSuccess(RedResult data) {
                if (isFinishing()) {
                    return;
                }
                sendRedMessage(num, finalNote,totalAmount,data.getId());
            }

            @Override
            public void onUiFailure(int code, String msg) {
                ToastUtils.showShort("发送失败"+msg);
                if (isFinishing()) {
                    return;
                }
            }
        });
    }
    private void sendRedMessage(String num,String note,double totalAmount,String packageId){
        TextMessageContent content = new TextMessageContent("[红包消息]");
        JSONObject extraJson = new JSONObject();
        try {
            extraJson.put("id",packageId);
            extraJson.put("amount",totalAmount);
            extraJson.put("few",num);
            extraJson.put("type",redType);
            extraJson.put("title",note);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String extraString = extraJson.toString();
        Message extraMsg = new Message();
        extraMsg.content = content;
        extraMsg.content.extra = extraString;
        Intent intent = new Intent();
        intent.putExtra("content",content);
        setResult(Activity.RESULT_OK,intent);
        finish();
    }
    @OnClick(R2.id.btSend)
    public void onClick(){
        PassWordDialog passWordDialog = new PassWordDialog(this);
        passWordDialog.setCallback(new AppService.CustomSimpleCallback() {
            @Override
            public void onUiSuccess(String result) {
                sendRed(result);
            }

            @Override
            public void onUiFailure(int code, String msg) {

            }
        });
        passWordDialog.show();
    }
}